/*
 Problem Description
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!
Input
One N in one line, process to the end of file.
Output
For each N, output N! in one line.
Sample Input
1
2
3
Sample Output
1
2
6
 */
package com.yuan.algorithms.practice201505;

import java.io.BufferedInputStream;
import java.io.PrintWriter;
import java.util.Scanner;

public class 大数阶乘 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(new BufferedInputStream(System.in));
		PrintWriter out = new PrintWriter(System.out);
		while(sc.hasNext()) {
			int n = sc.nextInt();
			out.println(factorial(n));
			out.flush();
		}
	}

	private static String factorial(int n) {
		if (n==0) {
			return "1";
		}
		StringBuffer sb = new StringBuffer();
        int max = 50000;
        int[] sum = new int[max];
        //获取n的每个位
        for (int i = -1, k = n; k > 0; k /= 10) {
            sum[++i] = k % 10;
        }
        //模拟阶乘运算
        for (int i = n - 1; i > 1; i--) {
            for (int j = 0; j < sum.length; j++) {
                sum[j] *= i;
            }
            //进位
            for (int j = 0; j < sum.length - 1; j++) {
                sum[j + 1] += sum[j] / 10;
                sum[j] %= 10;
            }
        }
        for (int i = sum.length - 1, test = 0; i >= 0; i--) {
            if (sum[i] != 0 || test != 0) {
                //result += sum[i];
            	sb.append(sum[i]);
                test = 1;
            }
        }
        return sb.toString();
	}

}
